package com.epam.kulibaba.components.PanelCriterias;

import java.util.HashSet;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Reporter;

import com.epam.kulibaba.components.Component;

public class PanelCornerCriterias extends Component {
	public static final String CORNERCRITERIAS = ".panel.corner.criteria";
	public static final String TOTAL = "div.total b";
	public static final String MINPRICES = "//div[@class = 'group'][1]/div[@class = 'is_empty_items']";
	public static final String MAXPRICES = "//div[@class = 'group'][2]/div[@class = 'is_empty_items']";
	public static final String SHOWALLPRODUCERS = "a.show_common_producer";
	public static final String LISTOFRODUCERS = "//div[@class = 'group'][3]/div[@class = 'is_empty_items']";
	public static final String LISTOFFUNCTIONS = ".//div[@class = 'is_empty_items']";
	
	@FindBy(css = PanelCornerCriterias.CORNERCRITERIAS)
	private WebElement cornerCriterias;
	
	@FindBy(xpath = PanelCornerCriterias.MINPRICES)
	private WebElement minPrices;
	
	@FindBy(xpath = PanelCornerCriterias.MAXPRICES)
	private WebElement maxPrices;
	
	@FindBy(css = PanelCornerCriterias.TOTAL)
	private WebElement total;
	
	@FindBy(css = PanelCornerCriterias.SHOWALLPRODUCERS)
	private WebElement linkShowAllProducers;
	
	@FindBy(xpath = PanelCornerCriterias.LISTOFRODUCERS)
	private WebElement ListOfproducers;
	
	
	@FindBy(xpath = PanelCornerCriterias.LISTOFFUNCTIONS)
	private WebElement listOfFunctions;
	
	public WebElement getCornerCriterias(){
		return cornerCriterias;
	}
	
	public WebElement getTotal(){
		return total;
	}
	public WebElement getMinPrices(){
		return minPrices;
	}
	
	public WebElement getMaxPrices(){
		return maxPrices;
	}	
	public WebElement getLinkShowAllProducers(){
		return linkShowAllProducers;
	}
	public WebElement getListOfproducers(){
		return ListOfproducers;
	}
		
	public WebElement getListOfFunctions(){
		return listOfFunctions;
	}
	
	public static PanelCornerCriterias getPanelCornerCriterias(WebDriver driver){
		PanelCornerCriterias panelCornerCriterias = PageFactory.initElements(driver, PanelCornerCriterias.class);
		return panelCornerCriterias;
	}	
	
	public static void setMinPrice(PanelCornerCriterias minPrices,
			double minPrice) {
		// TODO: check minPrice == maxPrice
		Reporter.log("set min price " + minPrice);
		List<WebElement> itemPrices = minPrices.getMinPrices().findElements(
				By.cssSelector("a"));
		for (WebElement price : itemPrices) {
			if (Double.parseDouble(price.getText()) == minPrice) {
				price.click();
				break;
			}
		}
	}

	public static void setMaxPrice(PanelCornerCriterias maxPrices,
			double maxPrice) {

		// TODO: check maxPrice == minPrice
		Reporter.log("set max price " + maxPrice);
		List<WebElement> itemPrices = maxPrices.getMaxPrices().findElements(
				By.cssSelector("a"));
		for (WebElement price : itemPrices) {
			if (Double.parseDouble(price.getText()) == maxPrice) {
				price.click();
				break;
			}
		}
	}

	public static void showAllProducers(
			PanelCornerCriterias linkShowAllProducers) {
		Reporter.log("Clik on link - show all producers");
		linkShowAllProducers.getLinkShowAllProducers().click();

	}

	public static HashSet<String> getAllProductsProducers(
			PanelCornerCriterias linkShowAllProducers,
			PanelCornerCriterias ListOfproducers) {
		Reporter.log("Get all product's producers from PanelCornerCriterias");
		HashSet<String> setProducers = new HashSet<String>();
		PanelCornerCriterias.showAllProducers(linkShowAllProducers);
		List<WebElement> otherNameProdicers = ListOfproducers
				.getListOfproducers().findElements(By.cssSelector("a.active"));
		for (WebElement name : otherNameProdicers) {
			setProducers.add(name.getText().toLowerCase());
		}
		// System.out.println("HashSet: \t" + setProducers.toString() + " (" +
		// setProducers.size() + ")");
		return setProducers;
	}
	
	public static List<WebElement> getFunctionsList(
			PanelCornerCriterias panelCornerCriterias) {
		Reporter.log("Get functions list for filtered products");
		return panelCornerCriterias.getCornerCriterias().findElements(By.tagName("a"));
	}
	
	public static void setFunctionFilter(PanelCornerCriterias panelCornerCriterias, String functionName){
		Reporter.log("Set function "+ functionName);
		List<WebElement> setFunctions = PanelCornerCriterias.getFunctionsList(panelCornerCriterias);		
		for (WebElement function : setFunctions) {
			if (function.getText().compareToIgnoreCase(functionName) == 0) {
				function.click();
				break;
			}
		}
	}	
}
